package com.cmcc.qx.ws.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.cmcc.qx.common.dao.impl.BaseDaoImpl;
import com.cmcc.qx.common.entity.Article;
import com.cmcc.qx.ws.dao.ArticleDao;

@Repository("ArticleDaoImpl")
public class ArticleDaoImpl extends BaseDaoImpl implements ArticleDao {

	Logger logger = Logger.getLogger(ArticleDaoImpl.class);
	public Article queryById(Integer id) {
		return (Article) this.getObj(Article.class, id);
	}

	public Integer queryCnt(Integer type) {
		String sql = "";
		if (type == 1) {
			sql = "select count(*) from xqarticle where sts='A' and top =0";
		} else {
			sql = "select count(*) from xqarticle where sts='A' and top =0 and type="
					+ type;
		}
		return Integer.valueOf(this.querySql(sql).get(0).toString());
	}

	public List<Article> queryTop() {
		String sql = "select id,title,left(onlytext,100) as onlytext,text,type,tag,visits,top,create_time,sts,picpath,pass_time from xqarticle a where top =1 and sts='A' limit 0,7";
		List<Object> list = this.querySqlClass(sql, Article.class);
		if (list != null) {
			List<Article> arts = new ArrayList<Article>();
			for (Object obj : list) {
				Article art = (Article) obj;
				arts.add(art);
			}
			return arts;
		}
		return null;
	}

	public List<Article> queryByDate(int start, int limit) {
		String hql = "select a from Article a where sts='A' and top = 0 order by createTime";
		List<Object> list = this.queryHql(start, limit, hql);
		if (list != null) {
			List<Article> arts = new ArrayList<Article>();
			for (Object obj : list) {
				Article art = (Article) obj;
				arts.add(art);
			}
			return arts;
		}
		return null;
	}

	public List<Article> queryByType(int start, int limit, Integer type) {
		String hql = null;
		List<Object> list = null;
		if (type == 1) {
			hql = "select a from Article a where sts='A' and top = 0 order by createTime desc";
			list = this.queryHql(start, limit, hql);
		} else {
			hql = "select a from Article a where sts='A' and top = 0 and type = ? order by createTime desc";
			list = this.queryHql(start, limit, hql, type);
		}
		if (list != null) {
			List<Article> arts = new ArrayList<Article>();
			for (Object obj : list) {
				Article art = (Article) obj;
				arts.add(art);
			}
			return arts;
		}
		return null;
	}

	public List<String> queryTag() {
		String sql = "SELECT DISTINCT tag FROM xqarticle WHERE id >= (SELECT FLOOR(MAX(id) * RAND())	FROM xqarticle) ORDER BY id LIMIT 30;";
		List<Object> list = this.querySql(sql);
		if (list != null) {
			List<String> tags = new ArrayList<String>();
			for (Object obj : list) {
				String str = obj.toString();
				tags.add(str);
			}
			return tags;
		}
		return null;
	}

	public List<Article> queryArt() {
		String hql = "from Article";
		List<Object> list = this.queryHql(hql);
		List<Article> arts = new ArrayList<Article>();
		if (list != null) {
			for (Object obj : list) {
				Article art = (Article) obj;
				arts.add(art);
			}
		}
		return arts;
	}

	public List<Article> queryRank() {
		String hql = "select a from Article a where sts='A' and top = 0 order by visits desc";
		List<Object> list = this.queryHql(0, 10, hql);
		if (list != null) {
			List<Article> arts = new ArrayList<Article>();
			for (Object obj : list) {
				Article art = (Article) obj;
				arts.add(art);
			}
			return arts;
		}
		return null;
	}

	public List<Article> selectArt(int start, int limit, String key) {
		String sql = "select * from xqarticle a where title like '%?%' ";
		List<Object> list = this.querySql(start,limit, sql,Article.class, key);
		if (list != null) {
			List<Article> arts = new ArrayList<Article>();
			for (Object obj : list) {
				Article art = (Article) obj;
				arts.add(art);
			}
			return arts;
		}
		return null;
	}

	public Integer selectCntBykey(String key) {
		String sql = "select count(*) from xqarticle a where title like '%?%' ";
		return Integer.valueOf(this.querySql(sql).get(0).toString());
	}

	@Override
	public List<Article> queryTagArt(int start, int limit, String tag) {
		String sql = "select id,title,left(onlytext,100) as onlytext,text,type,tag,visits,top,create_time,sts,picpath,pass_time from xqarticle a where sts='A'  and tag like '%"+tag+"%' order by create_time";
		List<Object> list = this.querySql(start,limit, sql, Article.class);
		if (list != null) {
			List<Article> arts = new ArrayList<Article>();
			for (Object obj : list) {
				Article art = (Article) obj;
				arts.add(art);
			}
			return arts;
		}
		return null;
	}

	public Integer queryTagArtCnt(String tag) {
		String sql = "select count(*) from xqarticle where sts='A' and tag like '%" + tag + "%'";
		return Integer.valueOf(this.querySql(sql).get(0).toString());
	}

	public void addVisits(String artid) {
//		String sql = "select max(visits) from xqarticle where id = ?";
//		int maxVisits = Integer.valueOf(this.querySql(sql, artid).get(0).toString());
//		maxVisits++;
//		String updateSql = "update xqarticle set visits = '"+ maxVisits +"' where id = "+ artid;
		String updateSql = "update xqarticle set visits = vistis + 1 where id = " + artid;
		this.sqlExecute(updateSql);
	}
}
